home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Personal Computer World 2009 February
/
PCWFEB09.iso
/
Software
/
FromTheMag
/
JW FLV MEDIA PLAYER 4.2
/
mediaplayer.exe
/
player.swf
/
scripts
/
com
/
jeroenwijering
/
models
/
SoundModel.as
< prev
next >
Wrap
Text File
|
2008-11-04
|
6KB
|
211 lines
package com.jeroenwijering.models
{
import com.jeroenwijering.events.ModelEvent;
import com.jeroenwijering.events.ModelStates;
import com.jeroenwijering.player.Model;
import flash.events.ErrorEvent;
import flash.events.Event;
import flash.events.IOErrorEvent;
import flash.events.ProgressEvent;
import flash.media.ID3Info;
import flash.media.Sound;
import flash.media.SoundChannel;
import flash.media.SoundLoaderContext;
import flash.media.SoundTransform;
import flash.net.URLRequest;
import flash.utils.clearInterval;
import flash.utils.setInterval;
public class SoundModel implements ModelInterface
{
private var channel:SoundChannel;
private var interval:Number;
private var sound:Sound;
private var duration:Number;
private var model:Model;
private var transform:SoundTransform;
private var context:SoundLoaderContext;
private var position:Number;
public function SoundModel(param1:Model)
{
super();
model = param1;
transform = new SoundTransform();
if(model.config["mute"] == true)
{
volume(0);
}
else
{
volume(model.config["volume"]);
}
context = new SoundLoaderContext(model.config["bufferlength"] * 1000);
}
public function stop() : void
{
clearInterval(interval);
if(channel)
{
channel.stop();
}
try
{
sound.close();
}
catch(err:Error)
{
}
}
private function timeHandler() : void
{
var _loc1_:* = undefined;
var _loc2_:* = undefined;
position = Math.round(channel.position / 100) / 10;
_loc1_ = Math.round(sound.length * sound.bytesTotal / sound.bytesLoaded / 100) / 10;
if(sound.isBuffering == true && sound.bytesTotal > sound.bytesLoaded)
{
if(model.config["state"] != ModelStates.BUFFERING)
{
model.sendEvent(ModelEvent.STATE,{"newstate":ModelStates.BUFFERING});
}
else
{
_loc2_ = Math.floor(sound.length / (channel.position + model.config["bufferlength"] * 1000) * 100);
model.sendEvent(ModelEvent.BUFFER,{"percentage":_loc2_});
}
}
else if(model.config["state"] == ModelStates.BUFFERING && sound.isBuffering == false)
{
model.sendEvent(ModelEvent.STATE,{"newstate":ModelStates.PLAYING});
}
if(_loc1_ > position)
{
model.sendEvent(ModelEvent.TIME,{
"position":position,
"duration":_loc1_
});
}
if(_loc1_ != duration && !isNaN(_loc1_))
{
duration = _loc1_;
model.sendEvent(ModelEvent.META,{"duration":duration});
}
}
private function completeHandler(param1:Event) : void
{
clearInterval(interval);
position = model.playlist[model.config["item"]]["start"];
model.sendEvent(ModelEvent.TIME,{
"position":position,
"duration":duration
});
model.sendEvent(ModelEvent.STATE,{"newstate":ModelStates.COMPLETED});
}
public function volume(param1:Number) : void
{
transform.volume = param1 / 100;
if(channel)
{
channel.soundTransform = transform;
}
}
private function progressHandler(param1:ProgressEvent) : void
{
var _loc2_:* = undefined;
var _loc3_:* = undefined;
_loc2_ = param1.bytesLoaded;
_loc3_ = param1.bytesTotal;
model.sendEvent(ModelEvent.LOADED,{
"loaded":_loc2_,
"total":_loc3_
});
}
private function errorHandler(param1:ErrorEvent) : void
{
model.sendEvent(ModelEvent.ERROR,{"message":param1.text});
stop();
}
public function load() : void
{
position = model.playlist[model.config["item"]]["start"];
duration = model.playlist[model.config["item"]]["duration"];
sound = new Sound();
sound.addEventListener(IOErrorEvent.IO_ERROR,errorHandler);
sound.addEventListener(ProgressEvent.PROGRESS,progressHandler);
sound.addEventListener(Event.ID3,id3Handler);
sound.load(new URLRequest(model.playlist[model.config["item"]]["file"]),context);
play();
}
private function id3Handler(param1:Event) : void
{
var id3:ID3Info = null;
var obj:* = undefined;
var evt:Event = param1;
try
{
id3 = sound.id3;
obj = {
"type":"id3",
"album":id3.album,
"artist":id3.artist,
"comment":id3.comment,
"genre":id3.genre,
"songName":id3.songName,
"track":id3.track,
"year":id3.year
};
model.sendEvent(ModelEvent.META,obj);
}
catch(err:Error)
{
}
}
public function play() : void
{
channel = sound.play(position * 1000,0,transform);
channel.removeEventListener(Event.SOUND_COMPLETE,completeHandler);
channel.addEventListener(Event.SOUND_COMPLETE,completeHandler);
interval = setInterval(timeHandler,100);
model.sendEvent(ModelEvent.STATE,{"newstate":ModelStates.PLAYING});
}
public function pause() : void
{
clearInterval(interval);
channel.stop();
model.sendEvent(ModelEvent.STATE,{"newstate":ModelStates.PAUSED});
}
public function seek(param1:Number) : void
{
clearInterval(interval);
position = param1;
channel.stop();
play();
}
public function quality(param1:Boolean) : void
{
}
}
}